package com.tj.view.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class MotionEventTest extends ImageView{

    private static final String TAG = "MotionEventTest";

    private int lastX =0;
    private int lastY =0;

    public MotionEventTest(Context context) {
        super(context);
    }

    public MotionEventTest(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MotionEventTest(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * view的滑动
     *
     * 注意点：获取getX时，要event.getX(),不是单独的getX()
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int offsetX =0;
        int offsetY =0;
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                offsetX = x- lastX;
                offsetY = y- lastY;
                //重新布局位置
                layout(getLeft()+offsetX, getTop()+offsetY, getRight()+offsetX,getBottom()+offsetY);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

//        return true;//如果需要拦截一下，返回true就不向下传递了,例如：点击事件就不起作用了
         return super.onTouchEvent(event);
    }
}
